package com.metaera.app.ui.adapters;

import android.content.Context;
import android.graphics.Color;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.ui.activities.ArticleDetailsActivity;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.SpannableStringHelper;

import androidx.annotation.NonNull;

public class SearchResultNewsAdapter extends BaseQuickAdapter<SearchResultResponse, BaseViewHolder> {

    private final String keyword;

    public SearchResultNewsAdapter(String keyword) {
        super(R.layout.item_search_news_type);
        this.keyword = keyword;

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(ArticleDetailsActivity.newInstance(context, getItem(position).getId()));
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, SearchResultResponse item) {
        holder.setText(R.id.tv_title, SpannableStringHelper.matchText(Color.parseColor("#3756FD"), item.getTitle(), keyword))
                .setText(R.id.tv_date,
                        item.getAuthor().getNickname()
                                + " · "
                                + DateUtils.getTime(item.getRelease_time_stamp(), DateUtils.PATTERN_8)
                );
    }



}
